package com.wxy;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        //集合只能存储引用类型
        TreeSet<Integer> set1 = new TreeSet<>();
        //添加元素，包装成Integer对象
        set1.add(5);
        set1.add(3);
        set1.add(4);
        set1.add(2);
        set1.add(1);
        for (Integer integer : set1){
            System.out.println(integer);
        }

        test1();
        test2();
        System.out.println(sum(1,2,3,4,5));
    }
    public static void test1() {
        TreeSet<Student> set = new TreeSet<>();
        
        set.add(new Student("罗欢",20));
        set.add(new Student("罗杨",19));
        set.add(new Student("罗燕妮",22));
        set.add(new Student("郑巧",21));
        for (Student student : set){
            System.out.println(student.getName() + "-------" + student.getAge());
        }
        
    }
    public static void test2() {
        Comparator<Student>comparator  =(o1,o2) ->  o1.getAge() - o2.getAge();
        TreeSet<Student> set = new TreeSet<>(comparator);
        set.add(new Student("罗欢",20));
        set.add(new Student("罗杨",19));
        set.add(new Student("罗燕妮",22));
        set.add(new Student("郑巧",21));
        for (Student student : set){
            System.out.println(student.getName() + "-------" + student.getAge());
        }
    }
    //可变参数
    //可变参数只能放在参数列表的最后
    public static int sum(int... a) {
        int total = 0;
        for (int i = 0;i < a.length;i++) {
            total += a[i];


        }return total;

    }

}
